package com.cychat.mall.controller;

import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.Orders;
import com.Cychat.commons.entity.pojo.Review;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cychat.mall.service.OrderDetailService;
import com.cychat.mall.service.OrdersService;
import com.cychat.mall.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Auther: 刘俊  <br/>
 * Date: 2025-11-9:下午 3:56  <br/>
 * Description: 评论控制器
 */
@RestController
@RequestMapping("/api/mall")
@CrossOrigin
public class ReviewController {
    @Autowired
    private ReviewService reviewService;
    @Autowired
    private OrdersService ordersService;

    //根据用户ID和商品ID写评论
    @PostMapping("/review/product/{userId}")
    public ResultMessage<String> addReview(
            @PathVariable("userId")Long userId,
            @RequestParam("orderId")String orderId,
            @RequestParam("productId")Integer productId,
            @RequestBody Review review) {
        try {
            if (userId == null || productId == null || review == null) {
                return ResultMessage.error("用户ID、商品ID或评论内容不能为空");
            }
            review.setUserId(userId);
            review.setProductId(productId);
            //判断商品评级是否在1-5分之间
            if (review.getGrade() < 1 || review.getGrade() > 5) {
                return ResultMessage.error("商品评级必须在1-5分之间");
            }
            reviewService.addReview(review);
            //查询orderId对应的订单，并将orderStatus设置为5（已评论）
            Orders orders = ordersService.getById(orderId);
            if (orders == null) {
                return ResultMessage.error("订单不存在");
            }
            orders.setOrderStatus(5);
            ordersService.updateById(orders);
            return ResultMessage.success("评论成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMessage.error("评论失败: " + e.getMessage());
        }
    }
}
